<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item
        ><a href="/categories">商品分类</a></el-breadcrumb-item
      >
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框 -->
      <!-- gutter每列相隔距离 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input clearable placeholder="请输入内容" v-model="ListForm.query" @clear="clearInput">
            <el-button slot="append" icon="el-icon-search" @click="clickSearch()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
        <!-- 表格区域 -->
      </el-row>
      <el-row>
        <el-table :data="ListData" border stripe style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column  label="创建时间">
            <template v-slot='scope'>
              {{scope.row.add_time*1000|dateFormat}}
            </template>
          </el-table-column>
          <!-- 操作面板 -->
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="editList(scope.row)"
                >编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="deleteList(scope.row)"
                >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页区域、 -->
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="ListData.pagenum"
          :page-sizes="[1, 5, 15, 30, 60]"
          :page-size="ListData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'List',
  data () {
    return {
      // 请求获取参数对象
      ListForm: {
        // 输入框文本
        query: '',
        pagenum: 1,
        pagesize: 1
      },
      // 列表数据
      ListData: [],
      total: 0
    }
  },
  mounted () {},
  created () {
    // 获取数据
    this.getListData()
  },
  methods: {
    getListData: async function () {
      const { data: res } = await this.$http.get('goods', {
        params: this.ListForm
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.ListData = res.data.goods
      this.total = res.data.total
    },
    // 监听每页数量的变化
    handleSizeChange: function (newSize) {
      this.ListForm.pagesize = newSize
      this.getListData()
    },
    // 监听页码值的变化
    // 监听页码值的变化
    handleCurrentChange: function (newPage) {
      this.ListForm.pagenum = newPage
      // 重新加载数据
      this.getListData()
    },
    // 编辑按钮事件
    editList: function () {
      console.log('我是编辑按钮')
    },
    // 删除按钮事件
    deleteList: async function (row) {
      // 询问是否确认删除
      const confirmRoles = await this.$confirm(
        '此操作将永久删除当前分类,是否继续',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      // 如果用户点击了确认了 这个异步函数返回字符串confirm
      // 如果用户点击了取消  这个异步函数返回字符串cancel
      if (confirmRoles !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      // 发送删除请求
      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      // 重新加载数据
      this.getListData()
    },
    // 点击搜索根据搜索框内容搜索商品
    clickSearch: function () {
      // 重新加载数据
      this.getListData()
    },
    // 当清空输入框内容是触发
    clearInput: function () {
      // 重新加载数据
      this.getListData()
    },
    // 添加商品
    goAddpage: function () {
      // 通过路由导航跳到添加商品组件
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table{
  margin-top: 15px;
}
</style>
